Array.prototype.mergeSort = function () {

    const split = (arr) => {
        if (arr.length === 1) {
            return arr;
        }
        const index = Math.floor(arr.length / 2);
        const left = arr.slice(0, index);
        const right = arr.slice(index, arr.length);
        const orderLeft = split(left);
        const orderRight = split(right);
        return merge(orderLeft, orderRight)
        // const res = [];
        // while (orderLeft.length || orderRight.length) {
        //     if (orderLeft.length && orderRight.length) {
        //         res.push(orderLeft[0] < orderRight[0] ? orderLeft.shift() : orderRight.shift());
        //     } else if (orderLeft.length && !orderRight.length) {
        //         res.push(orderLeft.shift());
        //     } else {
        //         res.push(orderRight.shift());
        //     }
        // }
        // return res;

    }

    console.log(split(this));

}
// 合并两个数组
const merge = function (orderLeft, orderRight) {
    const res = [];
    while (orderLeft.length || orderRight.length) {
        if (orderLeft.length && orderRight.length) {
            res.push(orderLeft[0] < orderRight[0] ? orderLeft.shift() : orderRight.shift());
        } else if (orderLeft.length && !orderRight.length) {
            res.push(orderLeft.shift());
        } else {
            res.push(orderRight.shift());
        }
    }
    return res;
}

// console.log(merge([1,2,5], [-4, 3]));


const arr = [5, 4, 3, 98, 2, 1, -4]
arr.mergeSort();